<?php

namespace Shared\LocalSyntaxSpecific;

/**
 * 
 * @author Jordan Minchev <j.minchev@object.bg>
 * @copyright (c) 2014, ASPECT.BG Ltd.
 */
class LocaleContentManager {

	private $tidyConfig = array(
		'clean' => true,
		'output-xhtml' => true,
		'show-body-only' => true,
		'wrap' => 0
	);

	/**
	 *
	 * @var ModifierInterface[]
	 */
	private $modifiers = [];

	public function init() {
		$Loader = new Loader;
		$Loader->init();
	}

	public function addModifier(ModifierInterface $modifier) {
		$this->modifiers[] = $modifier;
	}

	/**
	 * @param \Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity
	 */
	public function fix(\Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity) {
		$Metadata = $this->getEntityMetadata($Entity);
		$this->fixHTMLValues($Entity, $Metadata->getHtmlProperties());
		$this->fixPlainTextValues($Entity, $Metadata->getPlainTextProperties());
	}

	/**
	 * @param \Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity
	 * @return \Shared\LocalSyntaxSpecific\LRCModifier\ContentModifier
	 */
	private function getLRCModifier(\Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity) {
		$Language = $this->getLanguage($Entity);
		$className = $Language->getLRCModifierClassName();
		return new $className;
	}

	/**
	 * @param \Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity
	 * @return \Shared\Entity\Language
	 */
	private function getLanguage(\Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity) {
		$classname = \Doctrine\Common\Util\ClassUtils::getRealClass(get_class($Entity));
		$TranslationParser = new \Shared\LRC\Metadata\Parsers\Translation($classname, \Shared\Application::instance()->getEntityManager());
		$TranslationMetadata = $TranslationParser->parse();
		$languageProperty = $TranslationMetadata->getLanguagePropertyName();
		return $Entity->{'get' . ucfirst($languageProperty)}();
	}

	/**
	 * 
	 * @param \Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity
	 * @return \Shared\LocalSyntaxSpecific\Metadata\EntityMetadata
	 */
	private function getEntityMetadata(\Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity) {
		$Parser = new \Shared\LocalSyntaxSpecific\Metadata\Parser($Entity);
		return $Parser->parse();
	}

	/**
	 * @param \Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity
	 * @param array $properties
	 */
	private function fixHTMLValues(\Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity, array $properties) {
		$Modifier = $this->getLRCModifier($Entity);
		foreach ($properties as $propertyName) {
			$propertyName = ucfirst($propertyName);
			$textString = $Entity->{'get' . $propertyName}();
			$textString = $this->tidyHTML($textString);
                        
			$textString = $Modifier->clear($textString);
                        
			$HTMLParser = new \Shared\LocalSyntaxSpecific\HTML\Parser;
			$HTMLParser->load($textString);

			$Modifier->fixHTML($HTMLParser, $isHTML = true);

			$this->modifyHTML($HTMLParser);
			
			$HTMLParser->fillTags();

			$Entity->{'set' . $propertyName}($HTMLParser->getContent());
		}
	}

	/**
	 * 
	 * @param HTML\Parser $HTMLParser
	 * @return HTML\Parser
	 */
	private function modifyHTML($HTMLParser) {
		foreach ($this->modifiers as $modifier) {
			$modifier->fixHTML($HTMLParser);
		}
	}

	/**
	 * @param \Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity
	 * @param array $properties
	 */
	private function fixPlainTextValues(\Shared\LocalSyntaxSpecific\Entity\LocalSyntaxSpecificI $Entity, array $properties) {
		$Modifier = $this->getLRCModifier($Entity);
		foreach ($properties as $property) {
			$property = ucfirst($property);
                        $text = $Entity->{'get' . $property}();
                        $text = $Modifier->clear($text);
			$text = $Modifier->fixPlainText($text, $isHTML = true);
			$Entity->{'set' . $property}($text);
		}
	}

	private function tidyHTML($string) {
		$tidy = new \tidy;
		$tidy->parseString($string, $this->tidyConfig, 'utf8');
		return $tidy->value;
	}

}
